package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.ccl.iot.dvr.utils.HDImageManager;
import com.ccl.iot.object.IOTDeviceInfoEx;

public class IOTDoor extends IOTDeviceWithState {
    public static final int KDOOR_STATE_STOP = 0;
    public static final int KDOOR_STATE_OPEN = 1;
    public static final int KDOOR_STATE_CLOSE = 2;
    private final static String KSTATE_ID_LIST[] = {"0", "1", "2"};

    public IOTDoor(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aSwitchAddress, int aSwitchSize) {
        super(aDevID, aDeviceInfo, aSwitchAddress, aSwitchSize);
    }

    @Override
    public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
        saveRegister(aAddress, aSize, aData, aNetWork);
        super.updateRegister(aAddress, aSize, aData, aDataPos, aNetWork, time);
    }

    public int getPrevState() {
        return (int) (iState >> 16);
    }

    public int getCurState() {
        return (int) (iState & 0xFFFF);
    }

    @Override
    public String getStateID() {
        return String.valueOf(getCurState());
    }

    @Override
    public String[] getStateIDs() {
        return KSTATE_ID_LIST;
    }

    public void stop() {
        int tState = getCurState();

        if (tState != KDOOR_STATE_STOP) {
            setState((tState << 16) | KDOOR_STATE_STOP);
        }
    }

    public void open() {
        int tState = getCurState();

        if (tState != KDOOR_STATE_OPEN) {
            setState(KDOOR_STATE_OPEN);
        }
    }

    public void close() {
        int tState = getCurState();

        if (tState != KDOOR_STATE_CLOSE) {
            setState(KDOOR_STATE_CLOSE);
        }
    }

    public int getNextTurnState() {
        int tLastState = getPrevState();
        int tCurState = getCurState();

        if (tCurState == KDOOR_STATE_STOP) {
            if (tLastState == KDOOR_STATE_STOP || tLastState == KDOOR_STATE_CLOSE) {
                return KDOOR_STATE_OPEN;
            } else {
                return KDOOR_STATE_CLOSE;
            }
        } else {
            return KDOOR_STATE_STOP;
        }
    }

    public void turn() {
        int tState = getNextTurnState();

        if (tState == KDOOR_STATE_OPEN) {
            open();
        } else if (tState == KDOOR_STATE_CLOSE) {
            close();
        } else {
            stop();
        }
    }

    @Override
    public int getImageId() {
        int id = getSplitIconId();

        if (id >= DeviceIconProvider.ICON_LIST_DOOR_STOP.length) {
            id = 0;
        }

        switch (getCurState()) {
            case KDOOR_STATE_OPEN:
                return DeviceIconProvider.ICON_LIST_DOOR_OPEN[id];
            case KDOOR_STATE_STOP:
                return DeviceIconProvider.ICON_LIST_DOOR_STOP[id];
            case KDOOR_STATE_CLOSE:
                return DeviceIconProvider.ICON_LIST_DOOR_CLOSE[id];
        }

        return 0;
    }


    @Override
    public void onViewClick() {
        turn();
    }

    public String getImagePath() {

        if (getImageCount() > 0) {

            String imageID = getImageID(getStateID());

            String bitmapPath = HDImageManager.getInstince().getBitmapPath(imageID);

            if (TextUtils.isEmpty(bitmapPath)) {
                String stateIDs[] = getStateIDs();

                if (stateIDs != null) {
                    for (int i = 0; i < stateIDs.length; i++) {
                        if ((bitmapPath = HDImageManager.getInstince().getBitmapPath(getImageID(stateIDs[i]))) != null) {
                            break;
                        }
                    }
                }
            }

            return bitmapPath;
        }

        return null;
    }


    @Override
    public boolean setImage(Bitmap bitmap) {
        String imageID = getImageID(getStateID());

        if (bitmap != null) {
            return HDImageManager.getInstince().setBitmap(imageID, bitmap);
        }

        return false;
    }

    @Override
    public int getImageCount() {

        String imageIDs[] = getStateIDs();

        iImageCount = 0;
        if (imageIDs != null) {
            for (int i = 0; i < imageIDs.length; i++) {
                if (HDImageManager.getInstince().containsBitmap(getImageID(imageIDs[i]))) {
                    iImageCount++;
                }
            }
        }

        return iImageCount;

    }
}
